package com.jeff.android.lab.jetpack.viewpager2

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import butterknife.BindView
import butterknife.OnClick
import com.jeff.android.lab.BaseFragment
import com.jeff.android.lab.R
import com.jeff.utils.ToastUtil
import kotlin.random.Random

/**
 * @author: dingfeng
 * @date: 2020-12-01
 * @since: 1.0.0
 **/
class ViewPager2Fragment : BaseFragment() {

    @BindView(R.id.view_pager1)
    lateinit var mViewPager1: ViewPager2

    @BindView(R.id.view_pager2)
    lateinit var mViewPager2: ViewPager2

    override fun getRootLayout(): Int {
        return R.layout.jetpack_viewpager2_fragment
    }

    override fun initView() {
        mViewPager1.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                ToastUtil.show(mContext, "onPageSelected:$position")
            }
        })

        setTransform()
    }

    @OnClick(R.id.btn_direction)
    fun onDirection() {
        val orientation = mViewPager1.orientation
        mViewPager1.orientation = if (orientation == ViewPager2.ORIENTATION_VERTICAL) ViewPager2.ORIENTATION_HORIZONTAL else ViewPager2.ORIENTATION_VERTICAL

        ToastUtil.show(mContext, "修改滑动方向")
    }

    @OnClick(R.id.btn_disable)
    fun onDisable() {
        mViewPager1.isUserInputEnabled = !mViewPager1.isUserInputEnabled

        ToastUtil.show(mContext, "禁用/解禁滑动")
    }

    override fun initData() {
        val myData: List<String> = mutableListOf("Jack", "BeiBei", "Tom", "Android")

        val adapter = MyAdapter(myData)
        adapter.setData(myData)
        mViewPager1.adapter = adapter

        val adapter2 = MyAdapter(myData)
        adapter2.setData(myData)

        mViewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL
        mViewPager2.adapter = adapter2

    }

    fun setTransform() {

//        mViewPager1.apply {
//            offscreenPageLimit = 1
//            val recyclerView = getChildAt(0) as RecyclerView
//            recyclerView.apply {
//                val padding = resources.getDimensionPixelOffset(R.dimen.dp_10) +
//                        resources.getDimensionPixelOffset(R.dimen.dp_10)
//                // setting padding on inner RecyclerView puts overscroll effect in the right place
//                setPadding(padding, 0, padding, 0)
//                clipToPadding = false
//            }
//        }

        mViewPager1.offscreenPageLimit = 3

        val recyclerView = mViewPager1.getChildAt(0) as RecyclerView

        val padding = resources.getDimensionPixelOffset(R.dimen.dp_40) +
                resources.getDimensionPixelOffset(R.dimen.dp_40)
        // setting padding on inner RecyclerView puts overscroll effect in the right place
        recyclerView.setPadding(padding, 0, padding, 0)
        recyclerView.clipToPadding = false
        val compositePageTransformer = CompositePageTransformer()
        compositePageTransformer.addTransformer(ScaleInTransformer())
        compositePageTransformer.addTransformer(MarginPageTransformer(resources.getDimension(R.dimen.dp_10).toInt()))
        mViewPager1.setPageTransformer(compositePageTransformer)
    }


    class MyAdapter(var d: List<String>) : RecyclerView.Adapter<MyAdapter.PagerViewHolder>() {

        private lateinit var mData: List<String>

        public fun setData(data: List<String>) {
            mData = data
        }


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.jetpack_viewpager2_item, parent, false)
            return PagerViewHolder(itemView)
        }

        override fun getItemCount(): Int {
            return mData.size
        }

        override fun onBindViewHolder(holder: PagerViewHolder, position: Int) {
            holder.bindData(mData[position])
        }


        class PagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            private val mTextView: TextView = itemView.findViewById(R.id.tv)
            private var colors = arrayOf("#CCFF99", "#41F1E5", "#8D41F1", "#FF99CC")

            fun bindData(i: String) {
                mTextView.text = i
                mTextView.setBackgroundColor(Color.parseColor(colors[Random.nextInt(0, 3)]))
            }
        }

    }

}